define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'alarm/content/index',
                    add_url: 'alarm/content/add',
                    edit_url: '',
                    del_url: 'alarm/content/del',
                    multi_url: 'alarm/content/multi',
                    table: 'alarm_content',
                }
            });

            var table = $("#table");

            var state_arr = {"1":__('Send'),"0":__('NoSend'),"2":__('SendFail')};
            var alarm_way_arr = {"1":__('Mail'),"2":__('Wechat'),"3":__('DingTalk'),"9":__('Both')};
            var switch_arr = {"1":__('是'),"0":__('否')};
            var data_source_type_arr = {"1":__('告警配置'),"2":__('流量中断'),"3":__('接口状态变化'),"4":__('带宽告警'),"5":__('日报发送'),"6":__('ICMP告警')};
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'content.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'alarm_way', title: __('Alarm_way'), searchList:alarm_way_arr, formatter: function(all, row){
                            // console.log(value)
                            return alarm_way_arr[row['alarm_way']];
                        }},
                        {field: 'mail_status', title: __('mail_status'), searchList:state_arr, formatter: function(all, row){
                            // console.log(value)
                            if(row.alarm_way == 1 || row.alarm_way == 9){
                                return state_arr[row['mail_status']];
                            }else{
                                return '-';
                            }
                        }},
                        {field: 'wechat_status', title: __('wechat_status'), searchList:state_arr, formatter: function(all, row){
                            // console.log(value)
                            // console.log(value)
                            if(row.alarm_way == 2 || row.alarm_way == 9){
                                return state_arr[row['wechat_status']];
                            }else{
                                return '-';
                            }
                        }},
                        {field: 'dingtalk_status', title: __('DingTalk'), searchList:state_arr, formatter: function(all, row){
                            // console.log(value)
                            // console.log(value)
                            if(row.alarm_way == 3 || row.alarm_way == 9){
                                return state_arr[row['dingtalk_status']];
                            }else{
                                return '-';
                            }
                        }},
                        {
                            field: 'tag.name' , title: __('tag_id'),operate: 'LIKE',formatter: function(value,row,index){
                                if(value){
                                    if(row.tag){
                                        return row.tag.name;
                                    }else{
                                        return '-';
                                    }
                                }else{
                                    return '-';
                                }
                            }
                        },
                        // {
                        //     field: 'collectionagent.name', title: __('collectionagent_id'),formatter: function(value,row,index){
                        //         if(value){                                    
                        //             if(row.collectionagent){
                        //                 return row.collectionagent.name;
                        //             }else{
                        //                 return '-';
                        //             }
                        //         }else{
                        //             return '-';
                        //         }
                        //     }
                        // },
                        // {
                        //     field: 'device.name', title: __('device_id'),formatter: function(value,row,index){
                        //         if(value){ 
                        //             if(row.device){
                        //                 return row.device.name;
                        //             }else{
                        //                 return '-';
                        //             }
                        //         }else{
                        //             return '-';
                        //         }
                        //     }
                        // },
                        {
                            field: 'data_source_type',title: __('data_source_type'),searchList:data_source_type_arr,formatter: function(value,row,index){
                                return data_source_type_arr[value];
                                // data_source_type_arr
                            }
                        },
                        {field: 'content', title: __('Content'), operate: 'LIKE', placeholder: '模糊查询', cellStyle: formatTableUnit, formatter: paramsMatter},
                        {field: 'createtime', title: __('Createtime'), operate:false},
                        {field: 'ops_alarm_content.createtime', title: __('Createtime'),visible:false, operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:false},
                        {field: 'ops_alarm_content.updatetime', title: __('Updatetime'),visible:false, operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'handling_status',
                            // operate:false,
                            title: __('是否处理'),
                            align: 'center',
                            searchList:switch_arr,
                            formatter: Table.api.formatter.toggle
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('详情'),
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'alarm/content/detail',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });
            function paramsMatter(value, row, index, field) {                
                var span = document.createElement('span');
                var temp = document.createElement("div"); 
                temp.innerHTML = value; 
                var output = temp.innerText || temp.textContent; 
                temp = null; 
                span.setAttribute('title', output);
                span.innerHTML = value;
                return span.outerHTML;
            }
            function formatTableUnit(value, row, index) {
                return {
                    css: {
                        "white-space": "nowrap",
                        "text-overflow": "ellipsis",
                        "overflow": "hidden",
                        "max-width": "400px"
                    }
                }
            }
            

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});